<?php

namespace App\Http\Controllers\Admin;

use App\Models\JifenDetail;
use App\Models\ProdOrder;
use App\Models\Recharge;
use App\Models\Transition;
use App\Models\Withdraw;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;

/**
 * @group 后台接口文档
 * @title 消费记录模块接口
 * @author chenke
 * @description 酒庄后台消费记录板块接口
 * @header name:X-Token type:string require:1 desc:登录令牌
 *  */
class Cume extends Base
{
    /**
     * @title 消费记录列表
     * @description 后台消费记录数据
     * @url /admin/cume/cumeshow
     * @method GET
     * @module 交易记录板块
     * @author chenke
     * @param name:limit type:int require:1 default:1 other: desc:每页数据条数
     * @param name:page type:int require:1 default:1 other: desc:当前页数
     * @param name:searchcontent type:string require:0 default: other: desc:输入框搜索内容
     * @param name:searchtype type:string require:0 default: other: desc:输入框搜索类型(contact或email)
     * @param name:transtype type:int require:0 default: other:1为预定订单订金付款、2为预定订单结账付款、3为商品订单付款、9退款 desc:交易类型
     * @param name:uid type:int require:0 default: other: desc:用户ID
     * @return code:错误码
     * @return message:错误提示
     * @return data:交易信息@
     * @data id:交易ID transtype:交易类型 paytype:支付方式 transno:交易单号 username:用户英文名称 username_zn:用户中文名称  email:用户邮箱 contact:用户电话 total:用户累计消费 status:交易状态(1待交易2成功3退款4取消)
     */
    public function consumeList(Request $request){
        $data = $request->all();
        $limit = $data['limit'];
        $page = $data['page'];
        $searchCont = (isset($data['searchcontent']) && $data['searchcontent'])?$data['searchcontent']:'';
        $startDate = (isset($data['startDate']) && $data['startDate'])?$data['startDate']:date('Y-m-d');
        $endDate = (isset($data['endDate']) && $data['endDate'])?$data['endDate']:date('Y-m-d');
        $searchtype = (isset($data['searchtype']) && $data['searchtype'])?$data['searchtype']:'';
        $transtype = (isset($data['transtype']) && $data['transtype'])?$data['transtype']:0;
        $uid = (isset($data['uid']) && $data['uid'])?$data['uid']:0;
        $where = [];
        if($searchtype){
            $where[] = [$searchtype,'like','%'.$searchCont.'%'];
        }
        if($uid){
            $where[] = ['uid','=',$uid];
        }
        $wherein = [];
        if($transtype){
            $where[] = ['transtype','=',$transtype];
        }else{
            $wherein = [1,2,3,9];
        }
        $whereDates = [];
        if($endDate){
            $entime = strtotime($endDate) + 24*3600;
            $endDate = date('Y-m-d',$entime);
        }

        if($startDate && $endDate){
            $whereDates = [$startDate,$endDate];
        }

        $return = (new Transition())->getPageList(['limit'=>$limit,'page'=>$page],$where,$whereDates,$wherein);
        return response()->json($return);
    }

    /**
     * @title 充值记录列表
     * @description 后台充值记录
     * @url /admin/cume/cageshow
     * @method GET
     * @module 充值记录板块
     * @author chenke
     * @param name:limit type:int require:1 default:1 other: desc:每页数据条数
     * @param name:page type:int require:1 default:1 other: desc:当前页数
     * @param name:status type:int require:1 default: desc:充值状态 other:(1审核中、2审核成功、3审核失败)
     * @param name:searchcontent type:string require:0 default: other: desc:输入框搜索内容(对用户联系方式)
     * @param name:uid type:int require:0 default: other: desc:用户ID
     * @return code:错误码
     * @return message:错误提示
     * @return data:充值信息@
     * @data id:充值ID username:用户英文名 username_zn:用户中文名 number:充值金额 contact:用户电话 transtatus:交易状态(1审核中2审核成功3审核失败) evidence:用户凭证 bankcard:银行卡号 paybank:开户行 toname:付款对象
     */
    public function rechargeList(Request $request){
        $data = $request->all();
        $limit = $data['limit'];
        $page = $data['page'];
        $searchCont = (isset($data['searchcontent']) && $data['searchcontent'])?$data['searchcontent']:'';
        $startDate = (isset($data['startDate']) && $data['startDate'])?$data['startDate']:'';
        $endDate = (isset($data['endDate']) && $data['endDate'])?$data['endDate']:'';
        $status = (isset($data['status']) && $data['status'])?$data['status']:0;
        $uid = (isset($data['uid']) && $data['uid'])?$data['uid']:0;
        $where = [];
        if($uid){
            $where[] = ['uid','=',$uid];
        }
        if($searchCont){
            $where[] = ['contact','like','%'.$searchCont.'%'];
        }
        if($status){
            $where[] = ['transtatus','=',$status];
        }
        $whereDates = [];
        if($endDate){
            $entime = strtotime($endDate) + 24*3600;
            $endDate = date('Y-m-d',$entime);
        }
        if($startDate && $endDate){
            $whereDates = [$startDate,$endDate];
        }
        $return = (new Recharge())->getPageList(['limit'=>$limit,'page'=>$page],$where,$whereDates);
        return response()->json($return);
    }

    /**
     * @title 充值审核
     * @description
     * @url /admin/cume/cageset
     * @method POST
     * @module 充值记录板块
     * @author chenke
     * @param name:id type:int require:1 default:0 desc:充值ID other:
     * @param name:status type:int require:1 default: desc:要变成的充值状态 other:2审核成功3审核拒绝
     * @return code:错误码
     * @return message:错误提示
     * @return data:成功时为空
     */
    public function rechargeSet(Request $request){
        $data = $request->post();
        $rules = [
            'id'=>'required|integer',
            'status'=>[
                'required',Rule::in([2,3])
            ]
        ];
        $messages = [
            'id.required'=> '请传充值ID',
            'id.integer'=> '充值ID错误',
            'status.required'=> '请传审核状态',
            'status.integer'=> '审核状态错误'
        ];
        $check = $this->checkParams($data,$rules,$messages);
        if($check['code']){
            return response()->json($check);
        }
        $result =  (new Recharge())->examine($data);
        if($result['code']){
            $this->logErr($result['code'],$result['message'],'rechargeSet');
        }
        return response()->json($result);
    }

    /**
     * @title 提现记录列表
     * @description 后台提现记录
     * @url /admin/cume/widrshow
     * @method GET
     * @module 提现记录板块
     * @author chenke
     * @param name:limit type:int require:1 default:1 other: desc:每页数据条数
     * @param name:page type:int require:1 default:1 other: desc:当前页数
     * @param name:status type:int require:1 default: desc:提现状态 other:(1申请中、2提现通过、3提现拒绝)
     * @param name:searchcontent type:string require:0 default: other: desc:输入框搜索内容(对用户联系方式)
     * @param name:uid type:int require:0 default: other: desc:用户ID
     * @return code:错误码
     * @return message:错误提示
     * @return data:提现信息@
     * @data id:提现ID username:用户英文名 username_zn:用户中文名 number:提现金额 contact:用户电话 transtatus:交易状态(1审核中2审核成功3审核失败) evidence:提现凭证
     */
    public function withDrawList(Request $request){
        $data = $request->all();
        $limit = $data['limit'];
        $page = $data['page'];
        $searchCont = (isset($data['searchcontent']) && $data['searchcontent'])?$data['searchcontent']:'';
        $startDate = (isset($data['startDate']) && $data['startDate'])?$data['startDate']:'';
        $endDate = (isset($data['endDate']) && $data['endDate'])?$data['endDate']:'';
        $status = (isset($data['status']) && $data['status'])?$data['status']:0;
        $uid = (isset($data['uid']) && $data['uid'])?$data['uid']:0;
        $where = [];
        if($uid){
            $where[] = ['uid','=',$uid];
        }
        if($searchCont){
            $where[] = ['contact','like','%'.$searchCont.'%'];
        }
        if($status){
            $where[] = ['transtatus','=',$status];
        }
        $whereDates = [];
        if($endDate){
            $entime = strtotime($endDate) + 24*3600;
            $endDate = date('Y-m-d',$entime);
        }
        if($startDate && $endDate){
            $whereDates = [$startDate,$endDate];
        }
        $return = (new Withdraw())->getPageList(['limit'=>$limit,'page'=>$page],$where,$whereDates);
        return response()->json($return);
    }

    /**
     * @title 提现审核
     * @description
     * @url /admin/cume/drawset
     * @method POST
     * @module 提现记录板块
     * @author chenke
     * @param name:id type:int require:1 default:0 desc:提现ID other:
     * @param name:status type:int require:1 default: desc:要变成的提现状态 other:2提现成功3提现拒绝
     * @return code:错误码
     * @return message:错误提示
     * @return data:成功时为空
     */
    public function withdrawSet(Request $request){
        $data = $request->post();
        $rules = [
            'id'=>'required|integer',
            'status'=>[
                'required',Rule::in([2,3])
            ]
        ];
        $messages = [
            'id.required'=> '请传充值ID',
            'id.integer'=> '充值ID错误',
            'status.required'=> '请传审核状态',
            'status.integer'=> '审核状态错误'
        ];
        $check = $this->checkParams($data,$rules,$messages);
        if($check['code']){
            return response()->json($check);
        }
        $result =  (new Withdraw())->examine($data);
        if($result['code']){
            $this->logErr($result['code'],$result['message'],'rechargeSet');
        }
        return response()->json($result);
    }

    /**
     * @title 积分兑换记录列表
     * @description 后台积分兑换记录
     * @url /admin/cume/jfenshow
     * @method GET
     * @module 积分兑换板块
     * @author chenke
     * @param name:limit type:int require:1 default:1 other: desc:每页数据条数
     * @param name:page type:int require:1 default:1 other: desc:当前页数
     * @param name:status type:int require:1 default: desc:提现状态 other:(1待交易2成功3取消)
     * @param name:searchcontent type:string require:0 default: other: desc:输入框搜索内容(对用户联系方式)
     * @param name:uid type:int require:0 default: other: desc:用户ID
     * @return code:错误码
     * @return message:错误提示
     * @return data:积分兑换信息@
     * @data id:交易ID number:兑换数量 transtype:交易类型(.1为积分兑换、2为反馈积分) transname:交易名称 transno:交易单号 coin:交易后剩余积分 orderid:订单ID contact:用户电话
     */
    public function jifenList(Request $request){
        $data = $request->all();
        $limit = $data['limit'];
        $page = $data['page'];
        $searchCont = (isset($data['searchcontent']) && $data['searchcontent'])?$data['searchcontent']:'';
        $startDate = (isset($data['startDate']) && $data['startDate'])?$data['startDate']:date('Y-m-d');
        $endDate = (isset($data['endDate']) && $data['endDate'])?$data['endDate']:date('Y-m-d');
        $status = (isset($data['status']) && $data['status'])?$data['status']:0;
        $uid = (isset($data['uid']) && $data['uid'])?$data['uid']:0;
        $where = [];
        if($uid){
            $where[] = ['uid','=',$uid];
        }
        if($searchCont){
            $where[] = ['contact','like','%'.$searchCont.'%'];
        }
        if($status){
            $where[] = ['status','=',$status];
        }
        $whereDates = [];
        if($endDate){
            $entime = strtotime($endDate) + 24*3600;
            $endDate = date('Y-m-d',$entime);
        }
        if($startDate && $endDate){
            $whereDates = [$startDate,$endDate];
        }
        $return = (new JifenDetail())->getPageList(['limit'=>$limit,'page'=>$page],$where,$whereDates);
        return response()->json($return);
    }
}
